package cn.demomaster.quickevent.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class EventMessage<T extends Serializable> implements Serializable {
    private static final long serialVersionUID = 1L;

    private T code;
    private Map<String, Object> data;

    // 构造函数
    public EventMessage(T code) {
        this.code = code;
        this.data = new HashMap<>();
    }

    // Getter and Setter methods
    public T getCode() {
        return code;
    }

    public void setCode(T code) {
        this.code = code;
    }

    public Map<String, Object> getData() {
        return data;
    }

    // 添加参数的方法
    public void addData(String key, Object value) {
        if (value instanceof Serializable || value == null) {
            data.put(key, value);
        } else {
            throw new IllegalArgumentException("Value must be Serializable or null.");
        }
    }

    // 移除参数的方法
    public Object removeData(String key) {
        return data.remove(key);
    }

    // 获取特定参数的方法
    public Object getData(String key) {
        return data.get(key);
    }

    // 清空参数体
    public void clearData() {
        data.clear();
    }

    // toString 方法，用于调试
    @Override
    public String toString() {
        return "EventMessage{" +
                "code=" + code +
                ", data=" + data +
                '}';
    }
}
